package com.ca.ui.core.view.bar;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ui.core.R;
import com.ca.ui.core.util.ImageUtils;
import com.ca.ui.core.util.NumberUtils;

public class MiniClubTitleBar extends ConstraintLayout {

    private String imageUrl;
    private String name;
    private int followCount;

    public ImageView imageView;
    public TextView nameView;
    public TextView followCountView;

    public MiniClubTitleBar(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public MiniClubTitleBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public MiniClubTitleBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        View view = LayoutInflater.from(context).inflate(R.layout.mini_club_title_bar_layout, this);

        imageView = view.findViewById(R.id.iv);
        nameView = view.findViewById(R.id.tv_name);
        followCountView = view.findViewById(R.id.tv_followCount);

        if (attrs != null) {
            TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MiniClubTitleBar, 0, 0);

            imageUrl = array.getString(R.styleable.MiniClubTitleBar_mctb_imageUrl);
            name = array.getString(R.styleable.MiniClubTitleBar_mctb_name);
            followCount = array.getInteger(R.styleable.MiniClubTitleBar_mctb_followCount, 0);

            array.recycle();
        }

        setImageUrl(imageUrl);
        setName(name);
        setFollowCount(followCount);

        setOnClickListener(v -> {});
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl == null ? "" : imageUrl;
        ImageUtils.loadRoundImage(getContext(), this.imageUrl, imageView);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? "" : name;
        this.nameView.setText(this.name);
    }

    public int getFollowCount() {
        return followCount;
    }

    public void setFollowCount(int followCount) {
        this.followCount = followCount;
        String text = NumberUtils.numberToString((long) followCount) + "人关注";
        this.followCountView.setText(text);
    }
}
